package cn.quevo.core.lang;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018-03-09 16:04:02
 * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
 * @return
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    /**
     * 判断是否合法的日期
     *
     * @param str
     * @param pattern
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:05:32
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean isDate(String str, String... pattern) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        String patt = StringUtils.isBlank(pattern) || pattern.length <= 0 || StringUtils.isBlank(pattern[0])
                ? "yyyy-MM-dd" : pattern[0];
        return StringUtils.isBlank(DateFormatUtils.parse(str, patt)) ? false : true;
    }


    /**
     * 获得当前时间并格式化
     *
     * @param pattern
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:05:39
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static String getCurrentTime(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    /**
     * 比较str是否大于date
     *
     * @param str
     * @param date
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:05:48
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean after(String str, Date date) {
        Date d = DateFormatUtils.parse(str, "yyyy-MM-dd");
        return d != null && d.after(date);
    }

    /**
     * 比较str是否大于date
     *
     * @param str
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:05:55
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean after(String str) {
        return after(str, new Date());
    }

    /**
     * 比较str是否小于date
     *
     * @param str
     * @param date
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:06:04
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean before(String str, Date date) {
        Date d = DateFormatUtils.parse(str, "yyyy-MM-dd");
        return d != null && d.before(date);
    }

    /**
     * 比较str是否小于date
     *
     * @param str
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:06:13
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean before(String str) {
        return before(str, new Date());
    }

    /**
     * 获得当前日期的星期<br/>
     * Calendar以星期天做为第一天，而我们通常是星期一为第一天。<br/>
     * 我们通常认为的星期一，在Calendar中表示为2，星期天为1。
     *
     * @param calendar
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:06:21
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static int getDayOfWeek(Calendar calendar) {
        return calendar.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    /**
     * 根据时间匹配距离最近的星期
     *
     * @param date
     * @param weeks
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:06:29
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static Calendar getDateByWeek(Date date, int... weeks) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        for (int week : weeks) {
            calendar.set(Calendar.DAY_OF_WEEK, week + 1);
            if (!date.after(calendar.getTime())) {
                return calendar;
            }
        }
        calendar.set(Calendar.DAY_OF_WEEK, weeks[0]);
        calendar.add(Calendar.WEEK_OF_YEAR, 1);
        return calendar;
    }

    /**
     * 设置时间
     *
     * @param calendar
     * @param week
     * @param amount
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:06:37
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static void setDateByWeek(Calendar calendar, int week, int amount) {
        Date date = calendar.getTime();
        // 设置星期
        calendar.set(Calendar.DAY_OF_WEEK, week + 1);
        // 和设置星期之前的比较，如果设置星期之后的时间比之前的时间小，则增加一个星期
        if (date.after(calendar.getTime())) {
            calendar.add(Calendar.WEEK_OF_YEAR, 1);
        }
        if (amount > 0) {
            calendar.add(Calendar.WEEK_OF_YEAR, amount);
        }
    }

    /**
     * 获取两个日期之间的所有天数
     *
     * @param begin
     * @param end
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:06:45
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static List<Date> getDateRange(Date begin, Date end) {
        List<Date> result = new ArrayList<>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(begin);
        while (begin.getTime() <= end.getTime()) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
            begin = tempStart.getTime();
        }
        return result;
    }
}
